package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SaveBusinessVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ZnUserBusinessMapper;
import com.ruoyi.system.domain.ZnUserBusiness;
import com.ruoyi.system.service.IZnUserBusinessService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户模块关系Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-03-09
 */
@Service
public class ZnUserBusinessServiceImpl implements IZnUserBusinessService 
{
    @Autowired
    private ZnUserBusinessMapper znUserBusinessMapper;

    /**
     * 查询用户模块关系
     * 
     * @param id 用户模块关系主键
     * @return 用户模块关系
     */
    @Override
    public ZnUserBusiness selectZnUserBusinessById(Long id)
    {
        return znUserBusinessMapper.selectZnUserBusinessById(id);
    }

    /**
     * 查询用户模块关系列表
     * 
     * @param znUserBusiness 用户模块关系
     * @return 用户模块关系
     */
    @Override
    public List<ZnUserBusiness> selectZnUserBusinessList(ZnUserBusiness znUserBusiness)
    {
        znUserBusiness.setTenantId(SecurityUtils.getTenantId());
        return znUserBusinessMapper.selectZnUserBusinessList(znUserBusiness);
    }
     /**
      * 查询用户模块关系记录数
      *
      * @param znUserBusiness 用户模块关系
      * @return 用户模块关系记录数
     */
    public Integer selectZnUserBusinessCount(ZnUserBusiness znUserBusiness){
        znUserBusiness.setTenantId(SecurityUtils.getTenantId());
        return znUserBusinessMapper.selectZnUserBusinessCount(znUserBusiness);
    }


    /**
     * 新增用户模块关系
     * 
     * @param znUserBusiness 用户模块关系
     * @return 结果
     */
    @Override
    public int insertZnUserBusiness(ZnUserBusiness znUserBusiness)
    {
       znUserBusiness.setTenantId(SecurityUtils.getTenantId());
        znUserBusiness.setDeleteFlag("0");
        return znUserBusinessMapper.insertZnUserBusiness(znUserBusiness);
    }

    /**
     * 修改用户模块关系
     * 
     * @param znUserBusiness 用户模块关系
     * @return 结果
     */
    @Override
    public int updateZnUserBusiness(ZnUserBusiness znUserBusiness)
    {
        return znUserBusinessMapper.updateZnUserBusiness(znUserBusiness);
    }

    /**
     * 批量删除用户模块关系
     * 
     * @param ids 需要删除的用户模块关系主键
     * @return 结果
     */
    @Override
    public int deleteZnUserBusinessByIds(Long[] ids)
    {
        return znUserBusinessMapper.deleteZnUserBusinessByIds(ids);
    }

    /**
     * 删除用户模块关系信息
     * 
     * @param id 用户模块关系主键
     * @return 结果
     */
    @Override
    public int deleteZnUserBusinessById(Long id)
    {
        return znUserBusinessMapper.deleteZnUserBusinessById(id);
    }

    @Override
    @Transactional
    public int saveUserBusiness(SaveBusinessVO saveBusinessVO) {
        Long tenantId=SecurityUtils.getTenantId();
        saveBusinessVO.setTenantId(tenantId);
        String type=null;
        if(saveBusinessVO.getType().equals("1")){
            type="UserDepot";
        }else if(saveBusinessVO.getType().equals("2")){
            type="UserCustomer";
        }else {
            return -1;
        }
        saveBusinessVO.setType(type);
        ZnUserBusiness znUserBusiness= znUserBusinessMapper.selectZnUserBusinessByUserIdAndType(saveBusinessVO);
        int count=0;
        if (znUserBusiness==null){
            znUserBusiness=new ZnUserBusiness();
            znUserBusiness.setKeyId(saveBusinessVO.getUserId());
            znUserBusiness.setTenantId(tenantId);
            String symbol = StringUtils.join(saveBusinessVO.getValueList().toArray(), ",");
            znUserBusiness.setValue(symbol);
            znUserBusiness.setType(type);
            znUserBusiness.setDeleteFlag("0");
            count=znUserBusinessMapper.insertZnUserBusiness(znUserBusiness);
        }else{
            String symbol = StringUtils.join(saveBusinessVO.getValueList().toArray(), ",");
            znUserBusiness.setValue(symbol);
            count=znUserBusinessMapper.updateZnUserBusiness(znUserBusiness);
        }
        return count;
    }

    @Override
    public ZnUserBusiness selectZnUserBusinessByUserIdAndType(SaveBusinessVO saveBusinessVO) {
        return znUserBusinessMapper.selectZnUserBusinessByUserIdAndType(saveBusinessVO);
    }

}
